package de.baumann.browser.activitys;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.TextView;

import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.HashMap;
import java.util.Map;

import de.baumann.browser.ConstantKt;
import de.baumann.browser.R;
import de.baumann.browser.activitys.order.OdinPayActivity;
import de.baumann.browser.activitys.order.WechartPayActivity;
import de.baumann.browser.api.net.ApiKey;
import de.baumann.browser.api.net.vo.PayParam;
import de.baumann.browser.base.BaseOdinActivity;
import de.baumann.browser.base.BasePresenter;
import de.baumann.browser.iview.IBaseView;
import de.baumann.browser.js.AppJs;
import de.baumann.browser.js.PayJs;
import de.baumann.browser.utils.UserDataKt;
import de.baumann.browser.utils.ViewUtil;
import de.baumann.browser.web.OdinWebView;

/**
 * @author:
 * @date:2018/12/29 15:03
 * @discription: 加载 商城 /广告/规则/游戏/。。。。。
 */
public class H5Activity extends BaseOdinActivity implements AppJs.OnMallPayListener {
    private OdinWebView mX5WebView;
    private AppJs appJs;

    @Override
    protected int getLayout() {
        return R.layout.activity_h5;
    }

    protected boolean isHasTitle() {
        return getIntent().getBooleanExtra("isUseTitle", false);
    }

    @SuppressLint("JavascriptInterface")
    @Override
    protected void initView() {
        if (isHasTitle()) {
            findViewById(R.id.h5Title).setVisibility(View.VISIBLE);
            initToolbar(findViewById(R.id.commToolBar), null);
        }

        appJs = new AppJs(this);
        appJs.setMallPayListener(this);

        mX5WebView = findViewById(R.id.x5WebView);
        Map extraHeaders = new HashMap<String, String>(16);
        extraHeaders.put("Referer", "http://pay.voleedu.cn");
        mX5WebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                if (s.startsWith("weixin://wap/pay?")) {
                    if (ViewUtil.isInstalledWX(mContext)) {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(s));
                        startActivity(intent);
                    } else {
                        showShortToast("您还没有安装微信");
                    }
                    return true;
                }
                webView.addJavascriptInterface(appJs, "app");
                webView.addJavascriptInterface(new PayJs(() -> finish()), "pay");
                webView.loadUrl(s, extraHeaders);
                return true;
            }
        });
        mX5WebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView webView, String s, String s1, JsResult jsResult) {
                return super.onJsAlert(webView, s, s1, jsResult);
            }
        });
        mX5WebView.addJavascriptInterface(appJs, "app");
        mX5WebView.loadUrl(getIntent().getStringExtra("url"), extraHeaders);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        if (isHasTitle()) {
            int flag = getIntent().getIntExtra("flag", 0);
            switch (flag) {
                case 0:
                    ((TextView) findViewById(R.id.commTitle)).setText(R.string.about_odin);
                    break;
                case 1:
                    ((TextView) findViewById(R.id.commTitle)).setText(R.string.purchase_notes);
                    break;
                case 2:
                    ((TextView) findViewById(R.id.commTitle)).setText(R.string.licence_turns_right);
                    break;
                case 3:
                    ((TextView) findViewById(R.id.commTitle)).setText(getIntent().getStringExtra("title"));
                    break;
                case 4:
                    ((TextView) findViewById(R.id.commTitle)).setText(R.string.mining_agreement);
                    break;
                case 5:
                    ((TextView) findViewById(R.id.commTitle)).setText(R.string.preview);
                    break;
                case 6:
                    ((TextView) findViewById(R.id.commTitle)).setText(R.string.app_exchange);
                    break;
                case 7:
                    ((TextView) findViewById(R.id.commTitle)).setText("我的矿池");
                    break;
                default:
            }
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected IBaseView createView() {
        return null;
    }


    @Override
    public void onOdinPay(String tradeNo, String amount) {
        Intent intent = new Intent(mContext, OdinPayActivity.class);
        intent.putExtra("trandeNo", tradeNo);
        intent.putExtra("amount", amount);
        startActivityForResult(intent, 1417);
    }

    @Override
    public void onWechatPay(PayParam payParam) {
        Intent intent = new Intent(mContext, WechartPayActivity.class);
        intent.putExtra("payParam", payParam);
        startActivity(intent);
    }

    @Override
    public void onBackPressed() {
        if (mX5WebView.canGoBack()) {
            mX5WebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mX5WebView != null) {
            mX5WebView.destroy();
        }
    }


    public static void startMallActivity(Context context) {
        Intent mallIntent = new Intent(context, H5Activity.class);
        mallIntent.putExtra("url", ConstantKt.MALL_URL);
        context.startActivity(mallIntent);
    }

    public static void startPreviewUrlActivity(Context context, String url) {
        Intent intent = new Intent(context, H5Activity.class);
        intent.putExtra("isUseTitle", true);
        intent.putExtra("flag", 5);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    public static void startGameActivity(Context context, String url, String title) {
        Intent intent = new Intent(context, H5Activity.class);
        intent.putExtra("isUseTitle", true);
        intent.putExtra("flag", 3);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    public static void startAboutOdinActivity(Context context, String url) {
        Intent intent = new Intent(context, H5Activity.class);
        intent.putExtra("isUseTitle", true);
        intent.putExtra("flag", 0);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    public static void startPoolActivity(Context context) {
        Intent intent = new Intent(context, H5Activity.class);
        intent.putExtra("isUseTitle", true);
        intent.putExtra("flag", 7);
        intent.putExtra("url", ConstantKt.POOL_URL);
        context.startActivity(intent);
    }
    @Override
    public boolean showBindEth() {
        return false;
    }
}
